using System;
using System.Drawing;
using System.Reflection;
using Microsoft.Office.Core;
using TXLooker.Common.Outlook;

namespace TXLooker.Common.UI
{
    public delegate void UnsafeAction();

    public class OutlookUIFactory: IOutlookUIFactory
    {
        public CommandBarPopup CreateParentButton(CommandBar bar, string caption)
        {
            CommandBarPopup ctrl = (CommandBarPopup)bar.Controls.Add(MsoControlType.msoControlPopup, Missing.Value, Missing.Value, bar.Controls.Count == 0 ? 1 : bar.Controls.Count, true);
            ctrl.Caption = caption;
            ctrl.TooltipText = caption;
            ctrl.Visible = true;
            return ctrl;
        }

        public CommandBarPopup CreateParentForPupup(CommandBarPopup bar, string caption)
        {
            CommandBarPopup ctrl = (CommandBarPopup)bar.Controls.Add(MsoControlType.msoControlPopup, Missing.Value, Missing.Value, bar.Controls.Count, true);
            ctrl.Caption = caption;
            ctrl.TooltipText = caption;
            ctrl.Visible = true;
            return ctrl;
        }

        public CommandBarButton CreateChildButtonForPopup(CommandBarPopup parent, Bitmap pictureIcon, Bitmap maskIcon, string caption, OnMenuItemClick clickHandler)
        {
            CommandBarButton button = (CommandBarButton)parent.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, Missing.Value, true);
            button.Style = MsoButtonStyle.msoButtonIconAndCaption;
            button.Caption = caption;
            button.TooltipText = caption;
            button.Visible = true;
            button.Picture = AxHost2.GetIPictureDispFromPicture(pictureIcon);
            button.Mask = AxHost2.GetIPictureDispFromPicture(maskIcon);
            button.Click += delegate(CommandBarButton barButton, ref bool Cancel)
                            {
                                bool result = false;

                                ProtectOperation(delegate() { result = clickHandler(); });

                                Cancel = result;
                            };

            return button;
        }

        public CommandBarButton CreateChildButtonForBar(CommandBar parent, Bitmap pictureIcon, Bitmap maskIcon, string caption, OnMenuItemClick clickHandler)
        {
            CommandBarButton button = (CommandBarButton)parent.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, Missing.Value, true);
            button.Style = MsoButtonStyle.msoButtonIconAndCaption;
            button.Caption = caption;
            button.TooltipText = caption;
            button.Visible = true;
            button.Picture = AxHost2.GetIPictureDispFromPicture(pictureIcon);
            button.Mask = AxHost2.GetIPictureDispFromPicture(maskIcon);
            button.Click += delegate(CommandBarButton barButton, ref bool Cancel)
            {
                bool result = false;

                ProtectOperation(delegate() { result = clickHandler(); });

                Cancel = result;
            };

            return button;
        }

        public CommandBarComboBox CreateComboBox(CommandBar bar, string text, string description, string tooltip, int width, int dropDownLines, OnComboBoxSelected clickHandler)
        {
            CommandBarComboBox ctrl = (CommandBarComboBox)bar.Controls.Add(MsoControlType.msoControlComboBox, Missing.Value, Missing.Value, Missing.Value, true);
            ctrl.DescriptionText = description;
            ctrl.Text = text;
            //ctrl.BeginGroup = true;
            ctrl.TooltipText = tooltip;
            ctrl.Width = width;
            ctrl.Visible = true;
            ctrl.DropDownLines = dropDownLines;
            ctrl.Change += delegate(CommandBarComboBox comboBox)
            {
               ProtectOperation(delegate() { clickHandler(comboBox.Text); });
               comboBox.Text = description;
            };


            return ctrl;
        }

        private void ProtectOperation(UnsafeAction action)
        {
            try
            {
                action();
            }
            catch (Exception unhandledException)
            {
                LogManager.WriteMessageToCustomLog(unhandledException);
            }
        }
    }
}